package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import mest.dao.hws.warehouse_productjob_workfinishMapper;
import mest.dao.hws.warehouse_workfinishMapper;
import mest.service.cxj.DictionariesOrderService;
import mest.service.hws.warehouse_workfinishService;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 仓库流完工单
* */
@Service
public class warehouse_workfinishServiceImpl implements warehouse_workfinishService {

    @Resource
    private warehouse_workfinishMapper ww;

    @Resource
    private DictionariesOrderService dno;

    @Resource
    private warehouse_productjob_workfinishMapper wpw;

    //仓库库流完工单新增
    //返回id后继续完工内容单新增
    @Override
    public String insertModel(String theirDb,
                              List<Map<String, Object>> record) {
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        //采购单生成
        Map<String,Object> map1 = record.get(0);
        Map<String, Object> map = new HashMap<String, Object>();
        try {//自动生成流水号并set入对象
            System.out.println(dno.selectDictionariesOrder(theirDb).getPlanWorkfinishNumber());
            map1.put("tableNumber",("CKLCGD-"+dno.selectDictionariesOrder(theirDb).getPlanWorkfinishNumber()));
            map1.put("theirDb",theirDb);
            /* map1.put("id",0);*/
            System.out.println("调用完工汇报表新增方法");
            ww.insert(map1);
            System.out.println("生成仓库流完工汇报表"+map1);

            //采购单生成后返回数据
            //数据的id放进对象数组，调用内容表插入方法
            for (Map<String,Object> map2:record){
                map2.put("wwId",map1.get("id"));
                map2.put("theirDb",map1.get("theirDb"));
                //入库数量为操作记录入库数量，thisnumber
                map2.put("planNumber",map2.get("thisNumber"));
            }
            System.out.println("调用完工内容单新增方法");
            wpw.insert(theirDb,record);
            System.out.println("调用完工内容单新增方法");
            //操作成功后字典表+1
            dno.add_dictionaries_Order(theirDb,"planWorkfinishNumber");

            map.put("code", 0);
            map.put("msg", "操作成功");
            map.put("count",(record==null)?0:1 );
        }catch (Exception e){
            map.put("code", 1);
            map.put("msg", "操作失败");
            e.toString();
        }
        return JSON.toJSONString(map);
    }
}
